package cn.mafangui.hotel.controller.worker;

import cn.mafangui.hotel.entity.Msg;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.MsgType;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author ：Angular
 * @ProjectName: hotel
 * @Package: cn.mafangui.hotel.controller.worker
 * @ClassName: OpMsgController
 * @date ：Created in 2021/4/24 12:07
 * @description：留言管理
 * @modified By：
 * @version: v1.0.0$
 */
@RestController
@RequestMapping(value = "/op/msg")
public class OpMsgController {
    @Autowired
    private MsgService msgService;

    /**
     * 添加公告
     * 订单状态默认为未付款状态
     */
    @RequestMapping(value = "/add")
    public AjaxResult addNotice(String title, String content, @DateTimeFormat(pattern = "yyyy-MM-dd") Date time, String userId, String userName, String phone,String role) {
        Msg msg = new Msg(0,title, content, time, Integer.parseInt(userId), userName, phone,role);
        int re = msgService.insert(msg);
        if (re != 1) return ResponseTool.failed(MsgType.FAILED);
        return ResponseTool.success("添加成功.");
    }


    @RequestMapping(value = "/delete")
    public AjaxResult deleteNotice(int orderId) {
        int re = msgService.delete(orderId);
        if (re != 1) return ResponseTool.failed(MsgType.FAILED);
        return ResponseTool.success("删除成功.");
    }


    @RequestMapping(value = "/update")
    public AjaxResult updateNotice(int id, String title, String content) {
        Msg msg = new Msg(id, title, content);
        int re = msgService.update(msg);
        if (re != 1) return ResponseTool.failed(MsgType.FAILED);
        return ResponseTool.success("修改成功.");
    }


    @RequestMapping(value = "")
    public AjaxResult getAllMsg() {
        return ResponseTool.success(msgService.AllMsg());
    }

    @RequestMapping(value = "/count")
    public AjaxResult getMsgCount() {
        return ResponseTool.success(msgService.getMsgCount());
    }

    /**
     * 根据userID查询所有留言
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/user/{userId}")
    public AjaxResult getByUser(@PathVariable int userId) {
        return ResponseTool.success(msgService.selectByUserId(userId));
    }

    /**
     * 根据id号查询公告
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable Integer id) {
        return ResponseTool.success(msgService.selectById(id));
    }

}
